/**
 * @class 线填充类
 */
	
define(function(require,exports,module){
  var Stack = require("./base-modules/stack");
  var Point = require("./base-modules/Point")

  /**
   * @constructor 线填充类构造器
   * @param IDM 像素管理对象,由客户端提供
   */
  function LineFill(IDM) {
    this.s = new Stack();
    this.IDM = IDM;
  }

  module.exports = LineFill;

  /**
   * @method 填充函数
   * @param x 种子点的x坐标
   * @param y 种子点的y坐标
   * @param newColor 填充颜色
   * @param boundaryColor 边界颜色
   */
  LineFill.prototype.Fill = function(x,y,newColor,boundaryColor) {

    //种子点入栈
    this.s.Push(new Point(x, y));
    while (!this.s.Empty()) {
      //取当前种子点,并删除栈顶点
      var seed = this.s.Pop();

      //向左右填充
      var count = this._FillLineRight(seed.x, seed.y, newColor, boundaryColor);
      //因为右填充包括种子点,所以减1
      var xRight = seed.x + count - 1;
      count = this._FillLineLeft(seed.x - 1, seed.y, newColor, boundaryColor);
      var xLeft = seed.x - count;

      //处理相邻两条扫描线
      this._SearchLineNewSeed(xLeft, xRight, seed.y - 1, newColor, boundaryColor);
      this._SearchLineNewSeed(xLeft, xRight, seed.y + 1, newColor, boundaryColor);

      //复制像素到画布
      this.IDM.putImageToCanvas();
    }

   };

  /**
   * @method 向右填充颜色,包括种子点
   * @param x
   * @param y
   * @param newColor
   * @param boundaryColor
   * @returns {number}
   * @private
   */
  LineFill.prototype._FillLineRight = function(x,y,newColor,boundaryColor) {
    var count = 0;
    //如果当前点不相同就填充
    while(!this.IDM.getImageColor(x,y).isSameColor(boundaryColor)) {

      this.IDM.setImageColor(x,y,newColor);
      count++;
      x++;

    }
    return count;

  };

  /**
   * @method 向左填充颜色
   * @param x
   * @param y
   * @param newColor
   * @param boundaryColor
   * @returns {number}
   * @private
   */
  LineFill.prototype._FillLineLeft = function(x,y,newColor,boundaryColor) {
    var count = 0;
    while(!this.IDM.getImageColor(x,y).isSameColor(boundaryColor)) {
      this.IDM.setImageColor(x,y,newColor);
      count++;
      x--;
    }
    return count;

  };

  /**
   * @method
   * @param xLeft
   * @param xRight
   * @param y
   * @param newColor
   * @param boundaryColor
   * @private
   */
  LineFill.prototype._SearchLineNewSeed = function(xLeft,xRight,y,newColor,boundaryColor) {
    var xt = xLeft;
    var findNewSeed = false;


    //外层while确保被分成多段的正确处理
    while(xt <= xRight) {
      findNewSeed = false;
      //内层while确保每一段都找到一个种子点
      while(this._isPixelValid(xt,y,newColor,boundaryColor) && (xt<xRight)) {
        findNewSeed = true;
        xt++;
      }
      if(findNewSeed) {



        if(this._isPixelValid(xt,y,newColor,boundaryColor) && (xt == xRight)) {
          this.s.Push(new Point(xt,y));
        }
        else {
          this.s.Push(new Point(xt -1,y));
        }
      }

      //var xspan = SkipInvalidInLine(xt,y,xRight,newColor,boundaryColor);
      //xt += (xspan == 0) ? 1 :span;
      xt++;

    }

  };

  /**
   * @method 判断(x,y)是否为非边界且未填充
   * @param x
   * @param y
   * @param newColor
   * @param boundaryColor
   * @returns {boolean}
   * @private
   */
  LineFill.prototype._isPixelValid = function(x,y,newColor,boundaryColor) {
    var curColor = this.IDM.getImageColor(x,y);
    if(!curColor.isSameColor(newColor) &&
      !curColor.isSameColor(boundaryColor)) {
      return 	true;
    }
    else {
      return false;
    }
  };


  /**
   * @method 判断(x,y)是否在多边形内
   * @param x
   * @param y
   * @param boundaryColor
   * @returns {boolean}
   * @private
   */
  /*
  LineFill.prototype._isInPolygon = function(x,y,boundaryColor) {
    var leftX = x;
    while(!this.IDM.getImageColor(leftX,y).isSameColor(boundaryColor)) {
      if(leftX<=0) return false;
      leftX--;
    }
    var rightX = x;
    while(!this.IDM.getImageColor(rightX,y).isSameColor(boundaryColor)) {
      if(rightX >= 200) return false;
      rightX++;
    }
    return true;
  }
  */

});










